package com.njupt.sellsec.controller;

import com.njupt.sellsec.domain.SellUser;
import com.njupt.sellsec.domain.User;
import com.njupt.sellsec.result.CodeMsg;
import com.njupt.sellsec.result.Result;
import com.njupt.sellsec.service.Impl.SellUserServiceImpl;
import com.njupt.sellsec.service.SellUserService;
import com.njupt.sellsec.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: XJL
 * @Description:
 * @Date: Create in 14:52 2018/7/27
 * @Modified By:
 **/
@Controller
public class TestController {

    @Autowired
    private UserService userService;
    @Autowired
    private SellUserService sellUserService;


    @GetMapping("/hello")
    @ResponseBody
    public Result<User> hello(){
        User user = userService.getById(1);
        return Result.success(user);
    }

    @GetMapping("/error")
    @ResponseBody
    public Result<String> error(){
        return Result.error(CodeMsg.SERVER_ERROR);
    }

    @RequestMapping("/thymeleaf")
    public String thymeleaf(Model model){
        model.addAttribute("name","你是老王吧");
        return "hello";
    }

    @ResponseBody
    @RequestMapping("/user/info")
    public Result<SellUser> info(Model model,
                                 HttpServletResponse response,
                                 @CookieValue(value = SellUserServiceImpl.COOKIE_NAME_TOKEN,required = false)String cookieValue,
                                 @RequestParam(value = SellUserServiceImpl.COOKIE_NAME_TOKEN,required = false)String paramToken){
        if (StringUtils.isEmpty(cookieValue)&& StringUtils.isEmpty(paramToken)){
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        String token = StringUtils.isEmpty(paramToken)?cookieValue:paramToken;
        System.out.println("user info+++++++++++++++++++");
        SellUser user = sellUserService.findUserByToken(response,token);
        return Result.success(user);
    }


}

